home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ZPATH2.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  5KB  |  192 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zpath2.c */
  20. /* Non-constructor path operators for Ghostscript */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "errors.h"
  25. #include "alloc.h"
  26. #include "estack.h"    /* for pathforall */
  27. #include "gspath.h"
  28. #include "state.h"
  29. #include "store.h"
  30.  
  31. /* - flattenpath - */
  32. int
  33. zflattenpath(register os_ptr op)
  34. {    return gs_flattenpath(igs);
  35. }
  36.  
  37. /* - reversepath - */
  38. int
  39. zreversepath(register os_ptr op)
  40. {    return gs_reversepath(igs);
  41. }
  42.  
  43. /* - strokepath - */
  44. int
  45. zstrokepath(register os_ptr op)
  46. {    return gs_strokepath(igs);
  47. }
  48.  
  49. /* - clippath - */
  50. int
  51. zclippath(register os_ptr op)
  52. {    return gs_clippath(igs);
  53. }
  54.  
  55. /* - pathbbox <llx> <lly> <urx> <ury> */
  56. int
  57. zpathbbox(register os_ptr op)
  58. {    gs_rect box;
  59.     int code = gs_pathbbox(igs, &box);
  60.     if ( code < 0 ) return code;
  61.     push(4);
  62.     make_real(op - 3, box.p.x);
  63.     make_real(op - 2, box.p.y);
  64.     make_real(op - 1, box.q.x);
  65.     make_real(op, box.q.y);
  66.     return 0;
  67. }
  68.  
  69. /* <moveproc> <lineproc> <curveproc> <closeproc> pathforall - */
  70. private int path_continue(P1(os_ptr));
  71. private int path_cleanup(P1(os_ptr));
  72. int
  73. zpathforall(register os_ptr op)
  74. {    gs_path_enum *penum;
  75.     int code;
  76.     check_proc(op[-3]);
  77.     check_proc(op[-2]);
  78.     check_proc(op[-1]);
  79.     check_proc(*op);
  80.     check_estack(8);
  81.     if ( (penum = (gs_path_enum *)alloc(1, gs_path_enum_sizeof, "pathforall")) == 0 )
  82.         return_error(e_VMerror);
  83.     code = gs_path_enum_init(penum, igs);
  84.     if ( code < 0 )
  85.     {    alloc_free((char *)penum, 1, gs_path_enum_sizeof,
  86.                "path_cleanup");
  87.         return code;
  88.     }
  89.     /* Push a mark, the four procedures, and a pseudo-integer */
  90.     /* in which value.bytes points to the path enumerator, */
  91.     /* and then call the continuation procedure. */
  92.     mark_estack(es_for, path_cleanup);    /* iterator */
  93.     memcpy(esp + 1, op - 3, 4 * sizeof(ref));    /* 4 procs */
  94.     esp += 5;
  95.     make_tv(esp, t_integer, bytes, (byte *)penum);
  96.     pop(4);  op -= 4;
  97.     return path_continue(op);
  98. }
  99. /* Continuation procedure for pathforall */
  100. private int pf_push(P3(gs_point *, int, os_ptr));
  101. private int
  102. path_continue(register os_ptr op)
  103. {    gs_path_enum *penum = (gs_path_enum *)esp->value.bytes;
  104.     gs_point ppts[3];
  105.     int code;
  106.     code = gs_path_enum_next(penum, ppts);
  107.     switch ( code )
  108.       {
  109.     case 0:            /* all done */
  110.         { esp -= 6;
  111.           path_cleanup(op);
  112.           return o_pop_estack;
  113.         }
  114.     default:        /* error */
  115.         return code;
  116.     case gs_pe_moveto:
  117.         esp[2] = esp[-4];            /* moveto proc */
  118.         code = pf_push(ppts, 1, op);
  119.         break;
  120.     case gs_pe_lineto:
  121.         esp[2] = esp[-3];            /* lineto proc */
  122.         code = pf_push(ppts, 1, op);
  123.         break;
  124.     case gs_pe_curveto:
  125.         esp[2] = esp[-2];            /* curveto proc */
  126.         code = pf_push(ppts, 3, op);
  127.         break;
  128.     case gs_pe_closepath:
  129.         esp[2] = esp[-1];            /* closepath proc */
  130.         code = 0;
  131.         break;
  132.       }
  133.     if ( code < 0 ) return code;    /* ostack overflow in pf_push */
  134.     push_op_estack(path_continue);
  135.     ++esp;        /* include pushed procedure */
  136.     return o_push_estack;
  137. }
  138. /* Internal procedure to push one or more points */
  139. private int
  140. pf_push(gs_point *ppts, int n, os_ptr op)
  141. {    while ( n-- )
  142.       { push(2);
  143.         make_real(op - 1, ppts->x);
  144.         make_real(op, ppts->y);
  145.         ppts++;
  146.       }
  147.     return 0;
  148. }
  149. /* Clean up after a pathforall */
  150. private int
  151. path_cleanup(os_ptr op)
  152. {    gs_path_enum_cleanup((gs_path_enum *)esp[6].value.bytes);
  153.     alloc_free((char *)esp[6].value.bytes,
  154.            1, gs_path_enum_sizeof, "path_cleanup");
  155.     return 0;
  156. }
  157.  
  158. /* - initclip - */
  159. int
  160. zinitclip(register os_ptr op)
  161. {    return gs_initclip(igs);
  162. }
  163.  
  164. /* - clip - */
  165. int
  166. zclip(register os_ptr op)
  167. {    return gs_clip(igs);
  168. }
  169.  
  170. /* - eoclip - */
  171. int
  172. zeoclip(register os_ptr op)
  173. {    return gs_eoclip(igs);
  174. }
  175.  
  176. /* ------ Initialization procedure ------ */
  177.  
  178. op_def zpath2_op_defs[] = {
  179.     {"0clip", zclip},
  180.     {"0clippath", zclippath},
  181.     {"0eoclip", zeoclip},
  182.     {"0flattenpath", zflattenpath},
  183.     {"0initclip", zinitclip},
  184.     {"0pathbbox", zpathbbox},
  185.     {"4pathforall", zpathforall},
  186.     {"0reversepath", zreversepath},
  187.     {"0strokepath", zstrokepath},
  188.         /* Internal operators */
  189.     {"0%path_continue", path_continue},
  190.     op_def_end(0)
  191. };
  192.